package com.vip.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vip.base.constant.SysConsts;
import com.vip.base.service.ISysRoleService;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.Result;
import com.vip.common.response.Results;
import com.vip.common.util.UuidUtils;
import com.vip.database.dto.role.AddRoleDTO;
import com.vip.database.dto.role.GetRoleDTO;
import com.vip.database.entity.SysRole;
import com.vip.database.entity.SysRolePerm;
import com.vip.database.mapper.SysRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

/**
 *  服务实现类
 *
 * @author LEON
 * @since 2020-03-28
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public Result<String> addNewRole(AddRoleDTO dto) {
        try {
            if(roleMapper.roleCodeIsExist(dto.getCode())){
                return Results.newFailResult(ErrorCode.DB_ERROR, MessageFormat.format("添加失败：角色编码{0}已存在", dto.getCode()));
            }
            SysRole role = new SysRole();
            BeanUtils.copyProperties(dto, role);
            if (roleMapper.insert(role) == 1) {
                return Results.newSuccessResult(role.getId(), "添加成功");
            }
        } catch (Exception e) {
            log.error("新增角色出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "添加失败");
    }

    @Override
    public Result<List<GetRoleDTO>> getRoleList(Integer pageNo, Integer pageSize) {
        try {
            PageHelper.startPage(pageNo, pageSize);
            List<GetRoleDTO> result = roleMapper.getRoleList();
            PageInfo<GetRoleDTO> pageInfo = new PageInfo<>(result);
            return Results.newSuccessResult(pageInfo.getList(), "查询成功");
        } catch (Exception e) {
            log.error("获取角色列表数据出错！", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<List<GetRoleDTO>> getAllRoles() {
        try {
            List<GetRoleDTO> result = roleMapper.getAllRoles();
            return Results.newSuccessResult(result, "查询成功");
        } catch (Exception e) {
            log.error("获取所有角色数据出错！", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<GetRoleDTO> getRoleDetailsByRoleId(String roleId) {
        try {
            GetRoleDTO result = roleMapper.getRoleDetailsByRoleId(roleId);
            return Results.newSuccessResult(result, "查询成功");
        } catch (Exception e) {
            log.error("获取角色详情出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<String> updateRole(AddRoleDTO roleDto) {
        try {
            SysRole role = new SysRole();
            BeanUtils.copyProperties(roleDto, role);
            roleMapper.updateById(role);
            return Results.newSuccessResult(role.getId(), "更新成功");
        } catch (Exception e) {
            log.error("更新用户出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "更新失败");
    }

    @Override
    public Result<String> allotPermission(String roleId, String type, String permId) {
        try {
            if (SysConsts.OP_ADD.equals(type)) {
                SysRolePerm rolePerm = new SysRolePerm();
                rolePerm.setId(UuidUtils.uuid()).setRoleId(roleId).setPermId(permId);
                if (roleMapper.insertRolePerm(rolePerm) == 1) {
                    return Results.newSuccessResult(null, "授权成功");
                }
                return Results.newSuccessResult(null, "授权成功");
            } else if (SysConsts.OP_DEL.equals(type)) {
                if (roleMapper.deleteByRolePerm(roleId, permId) == 1) {
                    return Results.newSuccessResult(null, "释放成功");
                }
            } else {
                return Results.newFailResult(ErrorCode.INVALID_PARAMETER, "操作类型错误");
            }
        } catch (Exception e) {
            log.error("角色授权出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "操作失败");
    }
}
